package com.module.shici.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.module.shici.R;


public class AuthorRvAdapter extends RecyclerView.Adapter<AuthorRvAdapter.ViewHolder> {
    String[] authorArray;

    public AuthorRvAdapter(String[] authorArray) {
        this.authorArray = authorArray;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.module_shici_rv_shici_author,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.author.setText(authorArray[position]);
        holder.bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickLister!=null){
                    onItemClickLister.setOnItemClick(authorArray[holder.getLayoutPosition()]);
                }
            }
        });
    }

    public interface OnItemClickLister{
        void setOnItemClick(String author);
    }

    private OnItemClickLister onItemClickLister;

    public void getOnClick(OnItemClickLister onItemClickLister){
        this.onItemClickLister = onItemClickLister;
    }

    @Override
    public int getItemCount() {
        return authorArray.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView author;
        private ImageView bg;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            author = itemView.findViewById(R.id.rv_author);
            bg = itemView.findViewById(R.id.rv_author_bg);
        }
    }
}
